<style lang="less" scoped>
@import './login.less';
</style>

<template>
  <div class="login" @keydown.enter="handleSubmit">
    <div class="login-con">
      <Row>
        <Col span="24" class="login-form">
          <div class="form-title">
            <h2>后台登录</h2>
          </div>
          <Form ref="loginForm" :model="form" :rules="rules" label-position="top">
            <FormItem prop="username" >
              <Input  v-model="form.username" placeholder="请输入用户名">
                 <Icon type="md-contact" slot="prefix" />
              </Input>
            </FormItem>
            <FormItem prop="password">
              <Input  type="password" v-model="form.password" placeholder="请输入密码">
                 <Icon type="md-lock" slot="prefix" />
              </Input>
            </FormItem>
            <FormItem>
              <Row style="margin-top: 10%">
                <Col span="24">
                  <Button style="width:100%" size="large" @click="handleSubmit"  type="primary" >登录</Button>
                </Col>
              </Row>
            </FormItem>
          </Form>
        </Col>
      </Row>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      adminTitle:"商城系统",
      form: {
        username: this.Cookies.get('user'),
        password: this.Cookies.get('password')
      },
      rules: {
        username: [
          { required: true, message: '账号不能为空', trigger: 'blur' }
        ],
        password: [{ required: true, message: '密码不能为空', trigger: 'blur' }]
      }
    }
  },
  mounted() {
    console.log(this.menuRouter)
  },
  methods: {
    remember(data){
      if(data == true){
        this.Cookies.set('user', this.form.username)
        this.Cookies.set('password', this.form.password)
      }else{
        this.Cookies.remove('user')
        this.Cookies.remove('password')
      }
    },
    handleSubmit() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          let params = {
            account: this.form.username,
            password: this.form.password
          }
          this.$api.login(params).then(res => {
            console.log(res)
            if (res) {
              this.$Message.success('登录成功')
              this.Cookies.set('user', res.name)
              this.Cookies.set('session', res.session_id)
              console.log(this.Cookies.get('session'))
              this.$store.commit('setmenuPath', this.menuRouter[0].path)
              let redirect = decodeURIComponent(
                this.$route.query.redirect || '/home'
              )
              console.log(redirect)
              this.$router.push({
                path: redirect
              })
            } else {
              this.$Message.error('登陆失败，请检查账户名和密码')
            }
          })
        }
      })
    }
  }
}
</script>

<style lang="less"  scoped>

</style>


